<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <bro1></bro1>
        <bro2></bro2>
    </div>
</body>

</html>
<!-- 兄弟1 -->
<template id="bro1">
    <fieldset>
        <legend>兄弟1</legend>
        <input type="text" v-model="val">
        <button @click="handleClick">提交</button>
    </fieldset>
</template>
<!-- 兄弟2 -->
<template id="bro2">
    <fieldset>
        <legend>兄弟2</legend>
        <ul>
            <li v-for="(item,index) in arr">{{item}}</li>
        </ul>
    </fieldset>
</template>


<script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script>
<script>

    //所谓的eventBus就是new一个新的vue实例
    //vue2才有eventBus,vue3里面没有
    let eventBus = new Vue();

    Vue.component('bro1', {
        template: '#bro1',
        data: function () {
            return {
                val: ""
            }
        },
        methods: {
            handleClick() {
                eventBus.$emit('fm98', this.val)
                this.val = ""
            }
        }
    })

    Vue.component('bro2', {
        template: '#bro2',
        data: function () {
            return {
                arr: ['写代码']
            }
        },
        //收听一个频道(注册一个事件),事件的名字不能使用驼峰
        mounted() {
            //组件的钩子函数
            eventBus.$on('fm98', (input) => {
                console.log(input)
                this.arr.push(input)
            })
        }
    })


    let vm = new Vue({
        el: "#app"
    })
</script>